// src/directives/click-outside.js
export default {
  beforeMount(el, binding) {
    el.__clickOutsideHandler__ = (event) => {
      // console.log('点击了 document', event.target)
      // 检查点击的元素是否在指令绑定的元素内部
      if (!(el === event.target || el.contains(event.target))) {
        // console.log('触发绑定函数')
        binding.value(event)
      }
    }
    // 使用 setTimeout 确保在当前事件循环结束后再添加监听器
    setTimeout(() => {
      document.addEventListener('click', el.__clickOutsideHandler__)
    }, 0)
  },
  unmounted(el) {
    document.removeEventListener('click', el.__clickOutsideHandler__)
  }
}
